package com.wclass.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2017/4/24.
 */
public class DueDate {

    //计算每月账单日
    public int getmonthday(Date nowdate) {
        Date date = nowdate;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        /**
         计算还款日
         还款日规则：
         1、每月1-5号审核通过的还款日为次月1号；
         2、每月6-10号审核通过的还款日为次月6号；
         3、每月11-15号审核通过的还款日为次月11号；
         4、每月16-20号审核通过的还款日为次月16号；
         5、每月21-25号审核通过的还款日为次月21号；
         6、每月26-31号审核通过的还款日为次月26号；
         当   根据  还款日规则 来   更改每月还款日
         */
        //得到当前的日期
        int nowday = calendar.get(calendar.DAY_OF_MONTH);
        int mothday = 0;
        if (1 <= nowday && nowday <= 5) {
            calendar.set(calendar.DAY_OF_MONTH, 1);
        } else if (6 <= nowday && nowday <= 10) {
            calendar.set(calendar.DAY_OF_MONTH, 6);
        } else if (11 <= nowday && nowday <= 15) {
            calendar.set(calendar.DAY_OF_MONTH, 11);
        } else if (16 <= nowday && nowday <= 20) {
            calendar.set(calendar.DAY_OF_MONTH, 16);
        } else if (21 <= nowday && nowday <= 25) {
            calendar.set(calendar.DAY_OF_MONTH, 21);
        } else if (26 <= nowday && nowday <= 31) {
            calendar.set(calendar.DAY_OF_MONTH, 26);
        }
        mothday = calendar.get(calendar.DAY_OF_MONTH);
        return mothday;
    }

    //计算首次账单日
    public static Date getfirstday(Date nowtime) {
        Date date = nowtime;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        /**
         计算还款日
         还款日规则：
         1、每月1-5号审核通过的还款日为次月1号；
         2、每月6-10号审核通过的还款日为次月6号；
         3、每月11-15号审核通过的还款日为次月11号；
         4、每月16-20号审核通过的还款日为次月16号；
         5、每月21-25号审核通过的还款日为次月21号；
         6、每月26-31号审核通过的还款日为次月26号；
         当   根据  还款日规则 来   更改每月还款日
         */
        //得到当前的日期
        int nowday = calendar.get(calendar.DAY_OF_MONTH);
        int mothday = 0;
        if (1 <= nowday && nowday <= 5) {
            calendar.set(calendar.DAY_OF_MONTH, 1);
        } else if (6 <= nowday && nowday <= 10) {
            calendar.set(calendar.DAY_OF_MONTH, 6);
        } else if (11 <= nowday && nowday <= 15) {
            calendar.set(calendar.DAY_OF_MONTH, 11);
        } else if (16 <= nowday && nowday <= 20) {
            calendar.set(calendar.DAY_OF_MONTH, 16);
        } else if (21 <= nowday && nowday <= 25) {
            calendar.set(calendar.DAY_OF_MONTH, 21);
        } else if (26 <= nowday && nowday <= 31) {
            calendar.set(calendar.DAY_OF_MONTH, 26);
        }
        mothday = calendar.get(calendar.DAY_OF_MONTH);
        calendar.add(Calendar.MONTH, 1);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        calendar.set(Calendar.DATE, mothday - 1);
        calendar.set(Calendar.HOUR, 12);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    //计算首次账单日
    public static Date gethkday(Date nowtime) {
        Date date = nowtime;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //得到当前的日期
        int mothday = 0;
        calendar.set(calendar.DAY_OF_MONTH, 4);
        mothday = calendar.get(calendar.DAY_OF_MONTH);
        calendar.add(Calendar.MONTH, 2);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        calendar.set(Calendar.DATE, mothday - 1);
        calendar.set(Calendar.HOUR, 12);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    //计算最后账单日
    public static Date getlastday(Date nowtime, int stagenum) {
        Date date = nowtime;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        /**
         计算还款日
         还款日规则：
         1、每月1-5号审核通过的还款日为次月1号；
         2、每月6-10号审核通过的还款日为次月6号；
         3、每月11-15号审核通过的还款日为次月11号；
         4、每月16-20号审核通过的还款日为次月16号；
         5、每月21-25号审核通过的还款日为次月21号；
         6、每月26-31号审核通过的还款日为次月26号；
         当   根据  还款日规则 来   更改每月还款日
         */
        //得到当前的日期
        int nowday = calendar.get(calendar.DAY_OF_MONTH);
        int mothday = 0;
        if (1 <= nowday && nowday <= 5) {
            calendar.set(calendar.DAY_OF_MONTH, 1);
        } else if (6 <= nowday && nowday <= 10) {
            calendar.set(calendar.DAY_OF_MONTH, 6);
        } else if (11 <= nowday && nowday <= 15) {
            calendar.set(calendar.DAY_OF_MONTH, 11);
        } else if (16 <= nowday && nowday <= 20) {
            calendar.set(calendar.DAY_OF_MONTH, 16);
        } else if (21 <= nowday && nowday <= 25) {
            calendar.set(calendar.DAY_OF_MONTH, 21);
        } else if (26 <= nowday && nowday <= 31) {
            calendar.set(calendar.DAY_OF_MONTH, 26);
        }
        mothday = calendar.get(calendar.DAY_OF_MONTH);
        calendar.set(Calendar.MONTH, Calendar.MONTH + stagenum + 1);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        calendar.set(Calendar.DATE, mothday - 1);
        calendar.set(Calendar.HOUR, 12);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    /**
     * 计算时间间隔月数
     */
    public int getallmonth(Date nowtime, String time) throws ParseException {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int nowyear = calendar.get(Calendar.YEAR);
        ;
        int nowmonth = calendar.get(Calendar.MONTH) + 1;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dates = sdf.parse(time);
        Calendar calendars = Calendar.getInstance();
        calendars.setTime(dates);
        int year = calendars.get(Calendar.YEAR);
        int months = calendars.get(Calendar.MONTH) + 1;
        int allmonth = (nowyear - year) * 12 + (nowmonth - months);
        return allmonth;
    }

    public static void main(String[] args) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(df.format(DueDate.gethkday(new Date())));
    }

}